<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('基金管理费') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('年度')" prop="year">
        <DatePicker
          v-model="queryParams.year"
          type="year"
          value-format="yyyy"
          :placeholder="$t('年度')"
        />

      </el-form-item>
      <el-form-item :label="$t('计提周期')" prop="accrualPeriod">
        <el-select v-model="queryParams.accrualPeriod" :placeholder="$t('计提周期')" clearable>
          <el-option
            v-for="dict in accrualPeriodOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['fund:managementfee:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['fund:managementfee:edit']"
          type="warning"
          icon="el-icon-setting"
          size="mini"
          @click="feeConfigHandle"
        >{{ $t("费率配置") }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="fundManagementFeeList" :show-summary="true" :summary-method="getSummaries" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('年度')" align="center" prop="year" show-overflow-tooltip width="90">
        <template slot-scope="scope">
          <span class="link-type" @click="updateHandle(scope.row)">{{ scope.row.year }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('计提周期')" align="center" prop="accrualPeriod" :formatter="accrualPeriodFormat" show-overflow-tooltip width="110" />
      <el-table-column :label="$t('实缴金额') + $t('金额单位')" header-align="center" align="right" prop="beginningPaidInAmount" show-overflow-tooltip width="140">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.beginningPaidInAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('认缴金额') + $t('金额单位')" header-align="center" align="right" prop="subscribedAmount" show-overflow-tooltip width="200">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.subscribedAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('在投金额') + $t('金额单位')" header-align="center" align="right" prop="beginningReinvestedAmount" show-overflow-tooltip width="140">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.beginningReinvestedAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('应收管理费') + $t('金额单位')" header-align="center" align="right" prop="receivableManagementFee" show-overflow-tooltip width="200">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.receivableManagementFee, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('管理费计提说明')" align="center" prop="remark" min-width="200" show-overflow-tooltip />
      <el-table-column :label="$t('操作')" fixed="right" align="center" class-name="small-padding fixed-width" width="220">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-s-order"
            @click="fundManageFeeDetailHandle(scope.row)"
          >{{ $t('管理费明细') }}</el-button>
          <el-button
            v-hasPermi="['fund:managementfee:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['fund:managementfee:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改基金管理费对话框 -->
    <el-dialog :title="title" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <fundManagementFeeForm ref="fundManagementFeeForm" />
    </el-dialog>
    <!-- 添加或修改基金管理费对话框 -->
    <el-dialog :title="title" :visible.sync="listOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <fundManageFeeDetailList ref="fundManageFeeDetailList" />
    </el-dialog>
    <!-- 添加或修改基金管理费对话框 -->
    <el-dialog :title="title" :visible.sync="feeListOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogView" append-to-body>
      <fundManagementRateMainList ref="fundManagementRateMainList" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listFundManagementFee,
  delFundManagementFee,
  exportFundManagementFee,
  getFundManagementFeeSum
} from '@/api/fund/managementfee/fundManagementFee'
import fundManagementFeeForm from './fundManagementFeeForm.vue'
import fundManageFeeDetailList from '@/views/fund/manageFeeDetail/fundManageFeeDetailList'
import fundManagementRateMainList from '@/views/fund/managementfee/fundManagementRateMainList.vue'
export default {
    components: {
        fundManagementFeeForm,
        fundManageFeeDetailList,
      fundManagementRateMainList
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          feeListOpen: false,
          // 总条数
          total: 0,
          // 基金管理费表格数据
          fundManagementFeeList: [],
          fundManagementFeeSum: {},
          // 弹出层标题
          title: '',
          // 是否显示弹出层
          formOpen: false,
          listOpen: false,
        // 计提周期字典
        accrualPeriodOptions: [],
          // 查询参数
          queryParams: {
              fundId: this.$route.query.id,
              year: undefined,
              accrualPeriod: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {}
      }
  },
  created() {
      this.getList()
      this.getDicts('accrual_period_type').then(response => {
          this.accrualPeriodOptions = response.data
      })
  },
  methods: {
    /** 查询基金管理费列表 */
    getList() {
        this.loading = true
        getFundManagementFeeSum(this.queryParams).then(response => {
          this.fundManagementFeeSum = response.data
        })
        listFundManagementFee(this.queryParams).then(response => {
            this.fundManagementFeeList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 计提周期字典翻译
    accrualPeriodFormat(row) {
      return this.selectDictLabel(this.accrualPeriodOptions, row.accrualPeriod)
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            fundId: undefined,
            year: undefined,
            accrualPeriod: undefined,
            beginningPaidInAmount: undefined,
            subscribedAmount: undefined,
            beginningReinvestedAmount: undefined,
            investPeriod: undefined,
            exitPeriod: undefined,
            extensionPeriod: undefined,
            receivableManagementFee: undefined,
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.title = this.$t('添加基金管理费')
        this.$nextTick(() => {
            this.$refs.fundManagementFeeForm.init(null)
        })
    },
    /** 费率配置 */
    feeConfigHandle() {
        this.feeListOpen = true
        this.title = this.$t('费率配置')
        this.$nextTick(() => {
            this.$refs.fundManagementRateMainList.getList()
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.title = this.$t('修改基金管理费')
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.fundManagementFeeForm.init(row)
        })
    },
    /** 管理费明细 */
    fundManageFeeDetailHandle(row) {
        this.listOpen = true
        this.title = this.$t('管理费明细')
        this.$nextTick(() => {
            // 调用List页面的 getManageFeeDetail 方法初始化数据
            this.$refs.fundManageFeeDetailList.getManageFeeDetail(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delFundManagementFee(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return exportFundManagementFee(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(function() {
        })
    },
    // 合计
    getSummaries(param) {
      const { columns } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
        } else if (index === 3) {
          sums[index] = this.moneyFormat(this.fundManagementFeeSum.beginningPaidInAmount, 'w')
        } else if (index === 4) {
          sums[index] = this.moneyFormat(this.fundManagementFeeSum.subscribedAmount, 'w')
        } else if (index === 5) {
          sums[index] = this.moneyFormat(this.fundManagementFeeSum.beginningReinvestedAmount, 'w')
        } else if (index === 6) {
          sums[index] = this.moneyFormat(this.fundManagementFeeSum.receivableManagementFee, 'w')
        } else {
          sums[index] = ''
        }
      })
      return sums
    }
  }
}
</script>
